package test.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import by.grsu.hdm.devices.MediaPlayer;
import by.grsu.hdm.media.FileAudioVideo;

public class MediaPlayerBrowser extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setContentType("text/html");
		 
		 PrintWriter out = response.getWriter();
		 
		 out.println("<html>");
	     out.println("<head>");
	     out.println("<title>Media Player</title>");
	     out.println("<link type=\"text/css\" rel=\"stylesheet\" href=\"st/style.css\">");
	     out.println("</head>");
	     out.println("<body>");
	     
	     MediaPlayer mediaPlayer = new MediaPlayer("MP#22535");
	     	     
	     List<FileAudioVideo> filesAudio = new ArrayList<FileAudioVideo>();
	     List<FileAudioVideo> filesVideo = new ArrayList<FileAudioVideo>();
	     
	     FileAudioVideo audio1 = new FileAudioVideo("30 Seconds to Mars","mp3","#1111");
	     FileAudioVideo audio2 = new FileAudioVideo("System of a down","mp3","#2222");
	     FileAudioVideo audio3 = new FileAudioVideo("Muse","mp3","#3333");
	     FileAudioVideo audio4 = new FileAudioVideo("Joe Satriani","mp3","#4444");
	     
	     FileAudioVideo film1 = new FileAudioVideo("The Lone Ranger","AVI","#5555");
	     FileAudioVideo film2 = new FileAudioVideo("Corpse Bride","AVI","#6666");
	     FileAudioVideo film3 = new FileAudioVideo("Sinister","AVI","#7777");
	     FileAudioVideo film4 = new FileAudioVideo("Ghost","AVI","#8888");
	     
	     filesAudio.add(audio1);
	     filesAudio.add(audio2);
	     filesAudio.add(audio3);
	     filesAudio.add(audio4);
	     
	     filesVideo.add(film1);
	     filesVideo.add(film2);
	     filesVideo.add(film3);
	     filesVideo.add(film4);
	     
	     out.println("<div class=\"content\">");
	     
	     out.println("<div class=\"cap\">");
	     out.println("<h1>Media Player</h1>");
	     out.println("<h2>What play?</h2>");
	     out.println("</div>");
	     
	     out.println("<div class=\"player\">");
	     
	     out.println("<div class=\"container\">");
	     out.println("<div class=\"first\">");
	     out.println("<h3>Enter manually:</h3>");
	     out.print("<form action=\"MediaPlayerBrowser\" method=POST>");
	     out.println("File:  ");
	     out.println("<input type=\"text\" placeholder=\"name\" maxlength=\"20\" name=\"name\">");
	     out.println("<select name=\"format\">");
	     out.println("<option value=\"mp3\">mp3</option>");
	     out.println("<option value=\"AVI\">AVI</option>");
	     out.println("</select>");
	     out.println("<br/><br/>");
	     out.println("<input type=\"submit\" value=\"Enter\">");
	     out.println("</form>");	      
	     out.println("</div>"); 
	     out.println("</div>");
	     
	     out.println("<div class=\"container\">");
	     out.println("<div class=\"second\">");
	     out.println("<h3>Choose from a list:</h3>");
    	 out.println("<form action=\"MediaPlayerBrowser\" method=POST>");
	     out.println("<select name=\"format2\">");
	     out.println("<option value=\"audio\"> Audio </option>");
	     out.println("<option value=\"video\"> Video </option>");
	     out.println("</select>");
	     out.println("<br/><br/>");	     
	     out.println("<input type=\"submit\" value=\"select\">");
	     out.println("<br/>");
	     out.println("</form>");
	     
	     String fileFormat = request.getParameter("format2");
	     
	     if(fileFormat != null){
	    	 if ("audio".equals(fileFormat)){
	    		 out.println("<b>Audio</b><br/>");			  
	    		 out.println("<form action=\"MediaPlayerBrowser\" method=POST>");
	    		 for(FileAudioVideo file : filesAudio){
	    			 out.println("<input type=\"radio\" name=\"Q1\" value="+file.getId()+"> "+file.getName()+", "+file.getFormat()+" <br/>");
	    		 }
	    	     out.println("<input type=\"submit\" value=\"select\">");
	    	     out.println("</form>");
    	 	}	    	 
    		 if("video".equals(fileFormat)){
			     out.println("<b>Video</b><br/>");
			     out.println("<form action=\"MediaPlayerBrowser\" method=POST>");
			     for(FileAudioVideo file : filesVideo){
	    			 out.println("<input type=\"radio\" name=\"Q2\" value="+file.getId()+"> "+file.getName()+", "+file.getFormat()+" <br/>");
	    		 }
			     out.println("<input type=\"submit\" value=\"select\">");
			     out.println("</form>");
    		 }
	     }

	     out.println("</div>"); 
	     out.println("</div>");
	     
	     out.println("</div>");	     
	     
	     String name = request.getParameter("name");
	     String format = request.getParameter("format");

	     out.println("<div class=\"result\">");
	     if(name != null && name != ""){
	    	 FileAudioVideo audioVideo = new FileAudioVideo(name,format,"id5567");
		   	 out.println("<b>");
		   	 out.println(mediaPlayer.play(audioVideo));
		   	 out.println("</b>");
		   	 out.println("</div>");
	     }
	     if(request.getParameter("Q1") != null){			    
			 for(FileAudioVideo file : filesAudio){
				 if((file.getId()).equals(request.getParameter("Q1"))){
			    	 out.println("<b>");
			    	 out.println(mediaPlayer.play(file));
			    	 out.println("</b>");
				 }
			 }	 
		 }
     
		 if(request.getParameter("Q2") != null){
			 for(FileAudioVideo file : filesVideo){
				 if((file.getId()).equals(request.getParameter("Q2"))){					     
			    	 out.println("<b>");
			    	 out.println(mediaPlayer.play(file));
			    	 out.println("</b>");
				 }
			 }
		 }		 		 
	     if((name == null || name == "") && request.getParameter("Q1") == null && request.getParameter("Q2") == null){
	    	 out.println("Enter the file name!");
	     }
	     out.println("</div>");
	          
	     out.println("</body>");     
	     out.println("</html>");
	     
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}
	
}
